package com.htandroid.htandroid.aty;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;

import com.ab.soap.AbSoapListener;
import com.ab.soap.AbSoapParams;
import com.ab.util.AbDialogUtil;
import com.ab.util.AbToastUtil;
import com.htandroid.htandroid.R;
import com.htandroid.htandroid.entity.Htzc_Pvouch;
import com.htandroid.htandroid.excel.ExcelAsset_zcpd;
import com.htandroid.htandroid.sys.InstorageURL;
import com.htandroid.htandroid.sys.User;
import com.htandroid.htandroid.sys.base_index;
import com.htandroid.htandroid.webservice.abXMLHtzcPvouchlist;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.onekeyshare.ShareContentCustomizeCallback;

import static com.htandroid.htandroid.constance.ConstanceServer.BaseUrl_New;
import static com.htandroid.htandroid.excel.ExcelGetDir.getExcelDir;

/**
 * Created by lkp on 2015/11/29.
 */
public class zcpd_index extends base_index {
    private ArrayList<Htzc_Pvouch> zcpvouchlist;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setAbContentView(R.layout.layout_zcpd_index);
        initview("资产盘点");
        initview2();
        getdata(0);
    }

    //初始化2
    @Override
    public void initview2() {
        btn_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                excelPath = getExcelDir() + File.separator + "资产盘点.xls";
                ExcelAsset_zcpd zcpd = new ExcelAsset_zcpd(zcpd_index.this, excelPath, zcpvouchlist, listtype);
                if (zcpvouchlist.size() == 0) {
                    AbToastUtil.showToast(getApplicationContext(), "列表中没有数据，不能分享");
                } else {
                    OnekeyShare oks = new OnekeyShare();
                    oks.setTitle("资产盘点.xls");
                    oks.setShareContentCustomizeCallback(new ShareContentCustomizeCallback() {
                        @Override
                        public void onShare(Platform platform,
                                            cn.sharesdk.framework.Platform.ShareParams paramsToShare) {
                            // TODO Auto-generated method stub
                            if (platform.getName().equals("Wechat")) {
                                paramsToShare.setShareType(Platform.SHARE_FILE);
                            }
                        }
                    });
                    imgPath = getExcelDir() + File.separator + "excel.png";
                    writeImg(imgPath);
                    oks.setImagePath(imgPath);
                    oks.setFilePath(excelPath);
                    oks.show(getApplicationContext());
                }
            }
        });

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //处理选状态
                linechoose("r", Integer.valueOf("" + id));
                //从position改为id,因为过滤之后的position不正确
                Htzc_Pvouch zcpvouch = zcpvouchlist.get((int) id);
                Intent intent = new Intent(getApplicationContext(), zcpd_detail.class);
                //传递参数
                Bundle bundle = new Bundle();
                bundle.putSerializable("zcpd", zcpvouch);
                intent.putExtras(bundle);
                intent.putExtra("listtype", listtype);//传递类型,下一界面便于控制
                startActivityForResult(intent, R.layout.layout_zcpd_detail / 200000);
            }
        });
    }

    //获取数据
    @Override
    public void getdata(int page) {
//        TreeMap<String, String> properties = new TreeMap<String, String>();
//        //参数
//        String[] parms = {"usercode", "username", "cacc_id", "iyear", "djtype"};
//        String[] vals = {User.uid, User.nickName, myApplication.getGs_ztid(), myApplication.getGs_year(), listtype};
//        //生成xml
//        BuildQgdListXMLService service = new BuildQgdListXMLService();
//        String xmlstring = service.buildXML(null, "zcpd_list", InstorageURL.NAME_SPACE, parms, vals, null);
//
//        ToolSOAPxml.callService("zcpd_list", xmlstring, new ToolSOAPxml.WebServiceCallBack() {
//            @Override
//            public void onSucced(String result) {
//                adatperDataList.clear();
//                ArrayList<Map<String, Object>> dataListForInit = new ArrayList<Map<String, Object>>();
//                if (result != null) {
//                    //解析xml文件,返回列表
//                    zcpvouchlist = XMLHtzcPvouchlist.paraseCommentInfors(result);
//
//                    for (int i = 0; i < zcpvouchlist.size(); i++) {
//                        //增加一个行选择状态初始化
//                        sa.getIsSelected().put(i, false);
//                        //==================
//                        Htzc_Pvouch zcpvouch = new Htzc_Pvouch();
//                        zcpvouch = zcpvouchlist.get(i);
//                        //                            aaa.add(item.getPropertyAsString(0));
//                        Map<String, Object> map = new HashMap<String, Object>();
//                        map.put("index", (i + 1));
//                        map.put("cvouchid", "单据号：" + zcpvouch.getCvouchid());
//                        map.put("cdept", "盘点日期：" + zcpvouch.getDdate());
//                        map.put("cnocheckreason",
//                                "盘点部门：" + zcpvouch.getSdepts());
//                        map.put("ddate",
//                                "");
//                        //更多数据
//                        map.put("data5",
//                                "盘点部门：" + zcpvouch.getSdepts());
//                        map.put("data6",
//                                "");
//                        map.put("data7",
//                                "");
//                        map.put("data8",
//                                "");
//                        dataListForInit.add(map);
//                    }
//                } else {
//                    AbToastUtil.showToast(getApplicationContext(), R.string.sqlerrormsg);
//                }
//                toolDataProvider.initData(dataListForInit);
//                loadList();
//                //mHandler.sendEmptyMessage(LIST_GET_FIRST_PAGE); //加载完毕发送指令显示第一页
//                //swipeRefreshLayout.setRefreshing(false);
//            }
//
//            @Override
//            public void onFailure(String result) {
//                //swipeRefreshLayout.setRefreshing(false);
//                AbToastUtil.showToast(getApplicationContext(), R.string.sqlerrormsg);
//                //ToolLog.log("err：" + result);
//            }
//        });

        String urlString = "http://" + InstorageURL.SERVERURL + BaseUrl_New;//旧版webservice地址
        AbSoapParams params = new AbSoapParams();
        params.put("arg0", User.uid);
        params.put("arg1", User.nickName);
        params.put("arg2", myApplication.getGs_ztid());
        params.put("arg3", myApplication.getGs_year());
        params.put("arg4", listtype);
        mAbSoapUtil.call(urlString, InstorageURL.NAME_SPACE, "ZcpdListUserFilter", params, new AbSoapListener() {
            //获取数据成功会调用这里
            @Override
            public void onSuccess(int statusCode, String content) {
                adatperDataList.clear();
                ArrayList<Map<String, Object>> dataListForInit = new ArrayList<Map<String, Object>>();
                if (content != null) {
                    zcpvouchlist = abXMLHtzcPvouchlist.paraseCommentInfors(content);
                    for (int i = 0; i < zcpvouchlist.size(); i++) {
                        //增加一个行选择状态初始化
                        sa.getIsSelected().put(i, false);
                        Htzc_Pvouch zcpvouch = new Htzc_Pvouch();
                        zcpvouch = zcpvouchlist.get(i);
                        Map<String, Object> map = new HashMap<String, Object>();
                        map.put("index", (i + 1));
                        map.put("cvouchid", "单据号：" + zcpvouch.getCvouchid());
                        map.put("cdept", "盘点日期：" + zcpvouch.getDdate());
                        map.put("cnocheckreason", "盘点部门：" + zcpvouch.getSdepts());
                        map.put("ddate", "");
                        //更多数据
                        map.put("data5", "盘点部门：" + zcpvouch.getSdepts());
                        map.put("data6", "");
                        map.put("data7", "");
                        map.put("data8", "");
                        dataListForInit.add(map);
                    }
                    toolDataProvider.initData(dataListForInit);
                    loadList();
                } else {
                    AbToastUtil.showToast(zcpd_index.this, R.string.sqlerrormsg);
                }
            }

            // 失败，调用
            @Override
            public void onFailure(int statusCode, String content,
                                  Throwable error) {
                AbToastUtil.showToast(zcpd_index.this, R.string.sqlerrormsg);
            }

            // 开始执行前
            @Override
            public void onStart() {
                //显示进度框
                AbDialogUtil.showProgressDialog(zcpd_index.this, 0, "正在加载...");
            }

            // 完成后调用，失败，成功
            @Override
            public void onFinish() {
                //移除进度框
                AbDialogUtil.removeDialog(zcpd_index.this);
            }
        });
    }

    //接收返回数据(判断是否需要刷新当前列表)
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (resultCode) {
            case R.layout.layout_kcapp_detail / 100000:
                String isref = data.getExtras().getString("isref");
                if (isref.equals("1")) {
                    //刷新列表
                    getdata(0);
                }
            default:
                //其它窗口的回传数据
                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
